package cn.cdtu.tibetplan.service.impl;

import java.util.List;

import cn.cdtu.tibetplan.mapper.RolemenuMapper;
import cn.cdtu.tibetplan.pojo.*;
import cn.cdtu.tibetplan.pojo.RolemenuExample.Criteria;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.github.pagehelper.PageHelper;
import cn.cdtu.tibetplan.service.RolemenuService;

import org.springframework.stereotype.Service;

/**
 * 服务实现层
 *
 * @author Administrator
 */
@Service
public class RolemenuServiceImpl implements RolemenuService {

    @Autowired
    private RolemenuMapper rolemenuMapper;

    /**
     * 查询全部
     */
    @Override
    public List<Rolemenu> findAll() {
        return rolemenuMapper.selectByExample(null);
    }

    /**
     * 按分页查询
     */
    @Override
    public PageResult findPage(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        Page<Rolemenu> page = (Page<Rolemenu>) rolemenuMapper.selectByExample(null);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 增加
     */
    @Override
    public void add(Rolemenu rolemenu) {
        rolemenuMapper.insert(rolemenu);
    }


    /**
     * 修改
     */
    @Override
    public void update(Rolemenu rolemenu) {
        rolemenuMapper.updateByPrimaryKeySelective(rolemenu);
    }

    /**
     * 根据ID获取实体
     *
     * @param id
     * @return
     */
    @Override
    public Rolemenu findOne(Long id) {
        return rolemenuMapper.selectByPrimaryKey(id.intValue());
    }

    /**
     * 批量删除
     */
    @Override
    public void delete(Long[] ids) {
        for (Long id : ids) {
            rolemenuMapper.deleteByPrimaryKey(id.intValue());
        }
    }


    @Override
    public PageResult findPage(Rolemenu rolemenu, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);

        RolemenuExample example = new RolemenuExample();
        Criteria criteria = example.createCriteria();

        if (rolemenu != null) {
            if(rolemenu.getMenuid()!=null){
                criteria.andMenuidEqualTo(rolemenu.getMenuid());
            }
            if(rolemenu.getRoleid()!=null){
                criteria.andMenuidEqualTo(rolemenu.getRoleid());
            }
        }
        Page<Rolemenu> page = (Page<Rolemenu>) rolemenuMapper.selectByExample(example);
        return new PageResult(page.getTotal(), page.getResult());
    }

    public List<Rolemenu> findByRoleId(Rolemenu roleMenu) {
        RolemenuExample example = new RolemenuExample();
        Criteria criteria = example.createCriteria();

        if (roleMenu != null) {
            if(roleMenu.getMenuid()!=null){
                criteria.andMenuidEqualTo(roleMenu.getMenuid());
            }
            if(roleMenu.getRoleid()!=null){
                criteria.andRoleidEqualTo(roleMenu.getRoleid());
            }
        }

        List<Rolemenu> rolemenus = rolemenuMapper.selectByExample(example);
        return rolemenus;
    }

    public void deleteByRoleId(int roleId){
        RolemenuExample example=new RolemenuExample();
        Criteria criteria = example.createCriteria();
        criteria.andRoleidEqualTo(roleId);
        rolemenuMapper.deleteByExample(example);
    }

}
